home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / role / ldmud-3.2-bin.lha / mud / mudlib / dhrystone.c next >
C/C++ Source or Header  |  2001-08-08  |  11KB  |  352 lines

  1. //===========================================================================
  2. // Dhrystone-2.1 Benchmark for LPC
  3. //
  4. // Don't take this too serious :-)                               Sven C. Dack
  5. //===========================================================================
  6.  
  7. #define Default_Number_Of_Runs 1000
  8.  
  9. #pragma strong_types
  10.  
  11. //===========================================================================
  12. // Support functions to simulate memory allocation and other stuff..
  13.  
  14. mixed *alloc_array_50()    { return allocate(50); }
  15.  
  16. mixed *alloc_array_50_50() {
  17.   mixed *arr;
  18.   int i;
  19.  
  20.   for (i = 50, arr = allocate(50); i--; arr[i] = allocate(50));
  21.   return arr;
  22. }
  23.  
  24. mapping alloc_record() {
  25.   return ([ "Ptr_Comp": 0, "Discr": 0, "var_1": ({ 0, 0, "" }) ]);
  26. }
  27.  
  28. mapping copy_record(mapping rec) {
  29.   mapping r;
  30.  
  31.   r = copy(rec);
  32.   r["var_1"] = ({}) + rec["var_1"];
  33.   return r;
  34. }
  35.  
  36. int times() {
  37.   int *ru;
  38.  
  39.   ru = rusage();
  40.   return 10 * (ru[0] + ru[1]);
  41. }
  42.  
  43. //===========================================================================
  44. // The global variables.
  45.  
  46. #define Ident_1    0
  47. #define Ident_2    1
  48. #define Ident_3    2
  49. #define Ident_4    3
  50. #define Ident_5    4
  51.  
  52. #define Enum_Comp  0
  53. #define Int_Comp   1
  54. #define Str_Comp   2
  55.  
  56. mapping Ptr_Glob, Next_Ptr_Glob;
  57. int     Int_Glob;
  58. int     Bool_Glob;
  59. int     Ch_1_Glob, Ch_2_Glob;
  60. mixed  *Arr_1_Glob;
  61. mixed  *Arr_2_Glob;
  62. int     Begin_Time, End_Time, User_Time;
  63. float   Microseconds, Dhrystones_Per_Second;
  64.  
  65. //===========================================================================
  66. // Prototypes all functions (including those defined later).
  67.  
  68. void Proc_1(mapping m1);
  69. void Proc_2(int i1);
  70. void Proc_3(mapping m1);
  71. void Proc_4();
  72. void Proc_5();
  73. void Proc_6(int i1, int i2);
  74. void Proc_7(int i1, int i2, int i3);
  75. void Proc_8(mixed *a1, mixed *a2, int i1, int i2);
  76. int  Func_1(int i1, int i2);
  77. int  Func_2(string s1, string s2);
  78. int  Func_3(int i1);
  79.  
  80. //===========================================================================
  81.  
  82. void main(int Number_Of_Runs) {
  83.   int    Int_1_Loc, Int_2_Loc, Int_3_Loc;
  84.   int    Ch_Index;
  85.   int    Enum_Loc;
  86.   string Str_1_Loc, Str_2_Loc;
  87.   int    Run_Index;
  88.  
  89.   if (Number_Of_Runs <= 0)
  90.     Number_Of_Runs = Default_Number_Of_Runs;
  91.  
  92.   Arr_1_Glob = alloc_array_50();
  93.   Arr_2_Glob = alloc_array_50_50();
  94.   Str_1_Loc  = "";
  95.   Str_2_Loc  = "";
  96.   Next_Ptr_Glob                = alloc_record();
  97.   Ptr_Glob                     = alloc_record();
  98.   Ptr_Glob["Ptr_Comp"]         = Next_Ptr_Glob;
  99.   Ptr_Glob["Discr"]            = Ident_1;
  100.   Ptr_Glob["var_1"][Enum_Comp] = Ident_3;
  101.   Ptr_Glob["var_1"][Int_Comp]  = 40;
  102.   Ptr_Glob["var_1"][Str_Comp]  = "DHRYSTONE PROGRAM, SOME STRING";
  103.   Str_1_Loc        = "DHRYSTONE PROGRAM, 1'ST STRING";
  104.   Arr_2_Glob[8][7] = 10;
  105.   write("Dhrystone Benchmark, Version 2.1 (Language: LPC)\n");
  106.   write("Execution starts, " + Number_Of_Runs + " runs through Dhrystone\n");
  107.   Begin_Time = times();
  108.   for (Run_Index = 1; Run_Index <= Number_Of_Runs; ++Run_Index) {
  109.     Proc_5();
  110.     Proc_4();
  111.     Int_1_Loc = 2;
  112.     Int_2_Loc = 3;
  113.     Str_2_Loc = "DHRYSTONE PROGRAM, 2'ND STRING";
  114.     Enum_Loc  = Ident_2;
  115.     Bool_Glob = !Func_2(Str_1_Loc, Str_2_Loc);
  116.     while (Int_1_Loc < Int_2_Loc) {
  117.       Int_3_Loc = 5 * Int_1_Loc - Int_2_Loc;
  118.       Proc_7(Int_1_Loc, Int_2_Loc, &Int_3_Loc);
  119.       Int_1_Loc += 1;
  120.     }
  121.     Proc_8(Arr_1_Glob, Arr_2_Glob, Int_1_Loc, Int_3_Loc);
  122.     Proc_1(Ptr_Glob);
  123.     for (Ch_Index = 'A'; Ch_Index <= Ch_2_Glob; ++Ch_Index) {
  124.       if (Enum_Loc == Func_1(Ch_Index, 'C')) {
  125.         Proc_6(Ident_1, &Enum_Loc);
  126.         Str_2_Loc = "DHRYSTONE PROGRAM, 3'RD STRING";
  127.         Int_2_Loc = Run_Index;
  128.         Int_Glob  = Run_Index;
  129.       }
  130.     }
  131.     Int_2_Loc = Int_2_Loc * Int_1_Loc;
  132.     Int_1_Loc = Int_2_Loc / Int_3_Loc;
  133.     Int_2_Loc = 7 * (Int_2_Loc - Int_3_Loc) - Int_1_Loc;
  134.     Proc_2(&Int_1_Loc);
  135.   }  
  136.   End_Time = times();
  137.   write("Execution ends.\n");
  138.   write("Final values of the variables used in the benchmark:\n\n");
  139.   printf("Int_Glob:            %d\n", Int_Glob);
  140.   printf("        should be:   %d\n", 5);
  141.   printf("Bool_Glob:           %d\n", Bool_Glob);
  142.   printf("        should be:   %d\n", 1);
  143.   printf("Ch_1_Glob:           %c\n", Ch_1_Glob);
  144.   printf("        should be:   %c\n", 'A');
  145.   printf("Ch_2_Glob:           %c\n", Ch_2_Glob);
  146.   printf("        should be:   %c\n", 'B');
  147.   printf("Arr_1_Glob[8]:       %d\n", Arr_1_Glob[8]);
  148.   printf("        should be:   %d\n", 7);
  149.   printf("Arr_2_Glob[8][7]:    %d\n", Arr_2_Glob[8][7]);
  150.   printf("        should be:   Number_Of_Runs + 10\n");
  151. #if 0
  152.   printf("Ptr_Glob->\n");
  153.   printf("  Ptr_Comp:          %O\n", Ptr_Glob["Ptr_Comp"]);
  154.   printf("        should be:   (implementation-dependent)\n");
  155. #endif
  156.   printf("  Discr:             %d\n", Ptr_Glob["Discr"]);
  157.   printf("        should be:   %d\n", 0);
  158.   printf("  Enum_Comp:         %d\n", Ptr_Glob["var_1"][Enum_Comp]);
  159.   printf("        should be:   %d\n", 2);
  160.   printf("  Int_Comp:          %d\n", Ptr_Glob["var_1"][Int_Comp]);
  161.   printf("        should be:   %d\n", 17);
  162.   printf("  Str_Comp:          %s\n", Ptr_Glob["var_1"][Str_Comp]);
  163.   printf("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
  164. #if 0
  165.   printf("Next_Ptr_Glob->\n");
  166.   printf("  Ptr_Comp:          %O\n", Next_Ptr_Glob["Ptr_Comp"]);
  167.   printf("        should be:   (implementation-dependent), same as above\n");
  168. #endif
  169.   printf("  Discr:             %d\n", Next_Ptr_Glob["Discr"]);
  170.   printf("        should be:   %d\n", 0);
  171.   printf("  Enum_Comp:         %d\n", Next_Ptr_Glob["var_1"][Enum_Comp]);
  172.   printf("        should be:   %d\n", 1);
  173.   printf("  Int_Comp:          %d\n", Next_Ptr_Glob["var_1"][Int_Comp]);
  174.   printf("        should be:   %d\n", 18);
  175.   printf("  Str_Comp:          %s\n", Next_Ptr_Glob["var_1"][Str_Comp]);
  176.   printf("        should be:   DHRYSTONE PROGRAM, SOME STRING\n");
  177.   printf("Int_1_Loc:           %d\n", Int_1_Loc);
  178.   printf("        should be:   %d\n", 5);
  179.   printf("Int_2_Loc:           %d\n", Int_2_Loc);
  180.   printf("        should be:   %d\n", 13);
  181.   printf("Int_3_Loc:           %d\n", Int_3_Loc);
  182.   printf("        should be:   %d\n", 7);
  183.   printf("Enum_Loc:            %d\n", Enum_Loc);
  184.   printf("        should be:   %d\n", 1);
  185.   printf("Str_1_Loc:           %s\n", Str_1_Loc);
  186.   printf("        should be:   DHRYSTONE PROGRAM, 1'ST STRING\n");
  187.   printf("Str_2_Loc:           %s\n", Str_2_Loc);
  188.   printf("        should be:   DHRYSTONE PROGRAM, 2'ND STRING\n");
  189.   printf("\n");
  190.   User_Time = End_Time - Begin_Time;
  191.   if (User_Time <= 0) {
  192.     printf("Your rusage() doesn't seem to work: User_Time is %d.\n", User_Time);
  193.     write("\n");
  194.   } else if (User_Time < (2*100)) {
  195.     write("Measured time too small to obtain meaningful results\n");
  196.     write("Please increase number of runs\n");
  197.     write("\n");
  198.   } else {
  199.     Microseconds = to_float(User_Time) / Number_Of_Runs;
  200.     Dhrystones_Per_Second = 1000.0 * Number_Of_Runs / User_Time;
  201.     printf ("Microseconds for one run through Dhrystone: ");
  202.     printf ("%6.1f \n", Microseconds);
  203.     printf ("Dhrystones per Second:                      ");
  204.     printf ("%6.1f \n", Dhrystones_Per_Second);
  205.   }
  206. }
  207.  
  208. void Proc_1(mapping Ptr_Val_Par) {
  209.   mapping Next_Record;
  210.  
  211.   Next_Record                    = Ptr_Val_Par["Ptr_Comp"];
  212.   Ptr_Val_Par["Ptr_Comp"]        = copy_record(Ptr_Glob);
  213.   Ptr_Val_Par["var_1"][Int_Comp] = 5;
  214.   Next_Record["var_1"][Str_Comp] = Ptr_Val_Par["var_1"][Str_Comp];
  215.   Next_Record["var_1"][Int_Comp] = Ptr_Val_Par["var_1"][Int_Comp];
  216.   Next_Record["Ptr_Comp"]        = Ptr_Val_Par["Ptr_Comp"];
  217.   Proc_3(&(Next_Record["Ptr_Comp"]));
  218.   if (Next_Record["Discr"] == Ident_1) {
  219.     Next_Record["var_1"][Int_Comp] = 6;
  220.     Proc_6(  Ptr_Val_Par["var_1"][Enum_Comp],
  221.        &(Next_Record["var_1"][Enum_Comp]));
  222.     Next_Record["Ptr_Comp"] = Ptr_Glob["Ptr_Comp"];
  223.     Proc_7(  Next_Record["var_1"][Int_Comp], 10,
  224.        &(Next_Record["var_1"][Int_Comp]));
  225.   } else  
  226.     Ptr_Val_Par = copy_record(Ptr_Val_Par["Ptr_Comp"]);
  227. }
  228.  
  229. void Proc_2(int Int_Par_Ref) {
  230.   int Int_Loc, Enum_Loc;
  231.  
  232.   Int_Loc = Int_Par_Ref + 10;
  233.   do  
  234.     if (Ch_1_Glob == 'A') {
  235.       Int_Loc -= 1;
  236.       Int_Par_Ref = Int_Loc - Int_Glob;
  237.       Enum_Loc = Ident_1;
  238.     } while (Enum_Loc != Ident_1);  
  239. }
  240.  
  241. void Proc_3(mapping Ptr_Ref_Par) {
  242.   if (Ptr_Glob != 0)
  243.     Ptr_Ref_Par = Ptr_Glob["Ptr_Comp"];
  244.   Proc_7(10, Int_Glob, &(Ptr_Glob["var_1"][Int_Comp]));
  245. }  
  246.  
  247. void Proc_4() {
  248.   int Bool_Loc;
  249.   Bool_Loc = Ch_1_Glob == 'A';
  250.   Bool_Glob = Bool_Loc | Bool_Glob;
  251.   Ch_2_Glob = 'B';
  252. }
  253.  
  254. void Proc_5() {
  255.   Ch_1_Glob = 'A';
  256.   Bool_Glob = 0 ;
  257. }
  258.  
  259. void Proc_6(int Enum_Val_Par, int Enum_Ref_Par) {
  260.   Enum_Ref_Par = Enum_Val_Par;
  261.   if (!Func_3(Enum_Val_Par))
  262.     Enum_Ref_Par = Ident_4;
  263.   switch (Enum_Val_Par) {
  264.   case Ident_1:
  265.     Enum_Ref_Par = Ident_1;
  266.     break;
  267.   case Ident_2:
  268.     if (Int_Glob > 100)
  269.       Enum_Ref_Par = Ident_1;
  270.     else
  271.       Enum_Ref_Par = Ident_4;
  272.     break;
  273.   case Ident_3:
  274.     Enum_Ref_Par = Ident_2;
  275.     break;
  276.   case Ident_4:
  277.     break;
  278.   case Ident_5:
  279.     Enum_Ref_Par = Ident_3;
  280.     break;
  281.   }
  282. }
  283.  
  284. void Proc_7(int Int_1_Par_Val, int Int_2_Par_Val, int Int_Par_Ref) {
  285.   int Int_Loc;
  286.  
  287.   Int_Loc = Int_1_Par_Val + 2;
  288.   Int_Par_Ref = Int_2_Par_Val + Int_Loc;
  289. }
  290.  
  291. void Proc_8 (mixed *Arr_1_Par_Ref, mixed *Arr_2_Par_Ref,
  292.         int Int_1_Par_Val, int Int_2_Par_Val) {
  293.   int Int_Index, Int_Loc;
  294.  
  295.   Int_Loc = Int_1_Par_Val + 5;
  296.   Arr_1_Par_Ref[Int_Loc]      = Int_2_Par_Val;
  297.   Arr_1_Par_Ref[Int_Loc + 1]  = Arr_1_Par_Ref[Int_Loc];
  298.   Arr_1_Par_Ref[Int_Loc + 30] = Int_Loc;
  299.   for (Int_Index = Int_Loc; Int_Index <= Int_Loc + 1; ++Int_Index)
  300.     Arr_2_Par_Ref[Int_Loc][Int_Index]  = Int_Loc;
  301.   Arr_2_Par_Ref[Int_Loc][Int_Loc - 1] += 1;
  302.   Arr_2_Par_Ref[Int_Loc + 20][Int_Loc] = Arr_1_Par_Ref[Int_Loc];
  303.   Int_Glob = 5;
  304. }
  305.  
  306. int Func_1(int Ch_1_Par_Val, int Ch_2_Par_Val) {
  307.   int Ch_1_Loc, Ch_2_Loc;
  308.  
  309.   Ch_1_Loc = Ch_1_Par_Val;
  310.   Ch_2_Loc = Ch_1_Loc;
  311.   if (Ch_2_Loc != Ch_2_Par_Val)
  312.     return Ident_1;
  313.   else {
  314.     Ch_1_Glob = Ch_1_Loc;
  315.     return Ident_2;
  316.   }
  317. }
  318.  
  319. int Func_2(string Str_1_Par_Ref, string Str_2_Par_Ref) {
  320.   int Int_Loc, Ch_Loc;
  321.  
  322.   Int_Loc = 2;
  323.   while (Int_Loc <= 2)
  324.     if (Func_1(Str_1_Par_Ref[Int_Loc], Str_2_Par_Ref[Int_Loc + 1]) == Ident_1) {
  325.       Ch_Loc = 'A';
  326.       Int_Loc += 1;
  327.     }
  328.   if (Ch_Loc >= 'W' && Ch_Loc < 'Z')
  329.     Int_Loc = 7;
  330.   if (Ch_Loc == 'R')
  331.     return 1;
  332.   else {
  333.     if (Str_1_Par_Ref > Str_2_Par_Ref) {
  334.       Int_Loc += 7;
  335.       Int_Glob = Int_Loc;
  336.       return 1;
  337.     } else
  338.       return 0;
  339.   }
  340. }
  341.  
  342. int Func_3(int Enum_Par_Val) {
  343.   int Enum_Loc;
  344.  
  345.   Enum_Loc = Enum_Par_Val;
  346.   if (Enum_Loc == Ident_3)
  347.     return 1;
  348.   else
  349.     return 0;
  350. }
  351.  
  352.